//
//  IGTopicTableViewCell.m
//  jingdongfang
//
//  Created by BOE on 2017/8/18.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGTopicTableViewCell.h"
#import "IGTopicModel.h"
#import "IGTopicCommentTableViewCell.h"
#import "IGTopicDetailCommentCell.h"
#import "IGArtWorksCardView.h"
#import "IGCommunityDetailVC.h"
#import "IntroViewController.h"
#import "LJLbutton.h"
#import "IGTopicDetailVC.h"
#import "XiangQingViewController.h"
#import "IGCommentBean.h"

@interface IGTopicTableViewCell () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIImageView *shareImageView;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UIButton *followBtn;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *playerLayerView;
@property (nonatomic, strong) UIImageView *videoIcon;
@property (nonatomic, strong) UIImageView *wmplayer;
@property (nonatomic, strong) IGTopicModel *topicModel;
@property (nonatomic, strong) IGArtWorksCardView *artWorksCardView;//关联的作品
@property (nonatomic, strong) UILabel *communityLabel;//圈子名称

@property (nonatomic, strong) UIButton *goodBtn;//点赞按钮
@property (nonatomic, strong) UILabel *goodNumLabel;//点赞数
@property (nonatomic, strong) UIButton *commentBtn;//评论按钮
@property (nonatomic, strong) UILabel *commentNumLabel;//评论数
@property (nonatomic, strong) NSMutableParagraphStyle *muStyle;

@property (nonatomic, copy) NSIndexPath *indexPath;

@end

@implementation IGTopicTableViewCell

+ (IGTopicTableViewCell *)cellWithTableView:(UITableView *)tableView {
    IGTopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kIGTopicTableViewCell];
    if (!cell) {
        cell = [[IGTopicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kIGTopicTableViewCell];
        cell.contentView.backgroundColor = [UIColor mainBackgroundColor];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        CGFloat kAvatarWidth = 30.f;
        
        __weak typeof(self) weakSelf = self;
        // 发布者头像
        self.headImageView = [[UIImageView alloc] init];
        self.headImageView.backgroundColor = [UIColor mainBackgroundColor];
        self.headImageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:self.headImageView];
        
        self.headImageView.clipsToBounds = YES;
        self.headImageView.userInteractionEnabled = YES;
        self.headImageView.layer.cornerRadius = kAvatarWidth/2;
        [self.headImageView setTapActionWithBlock:^{
            IntroViewController *introVC = [IntroViewController new];
            introVC.u_id = weakSelf.topicModel.userId;
            [UIViewController ig_pushViewController:introVC animated:YES];
        }];
        
        // 发布者姓名
        self.nameLabel = [UILabel new];
        [self.contentView addSubview:self.nameLabel];
        self.nameLabel.textColor = [UIColor c4Color];
        self.nameLabel.preferredMaxLayoutWidth = KSCREENWIDTH - 4 * kGAP15 - kAvatarWidth - 40;
        self.nameLabel.numberOfLines = 1;
        self.nameLabel.font = [UIFont font16];
        self.nameLabel.userInteractionEnabled = YES;
        
        [self.nameLabel setTapActionWithBlock:^{
            IntroViewController *introVC = [IntroViewController new];
            introVC.u_id = weakSelf.topicModel.userId;
            [UIViewController ig_pushViewController:introVC animated:YES];
        }];
        
        _followBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_followBtn];
        _followBtn.clipsToBounds=YES;
        _followBtn.layer.borderWidth=1.0f;
        _followBtn.layer.cornerRadius=15.f;
        _followBtn.layer.borderColor=[UIColor c1Color].CGColor;
        _followBtn.contentEdgeInsets=UIEdgeInsetsMake(5, 10, 5, 10);
        _followBtn.titleLabel.font=[UIFont font14];
        [_followBtn setTitleColor:[UIColor c5Color] forState:UIControlStateSelected];
        [_followBtn setTitleColor:[UIColor c1Color] forState:UIControlStateNormal];
        [_followBtn setTitle:NSLocalizedString(@"Followed", nil) forState:UIControlStateSelected];
        [_followBtn setTitle:[NSString stringWithFormat:@"+ %@",NSLocalizedString(@"Follow", nil)] forState:UIControlStateNormal];
        [_followBtn addTarget:self action:@selector(attentionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_followBtn setSelected:NO];
        
        //时间
        self.dateLabel = [UILabel new];
        self.dateLabel.numberOfLines = 0;
        self.dateLabel.textColor = [UIColor c5Color];
        self.dateLabel.font = [UIFont font12];
        self.dateLabel.preferredMaxLayoutWidth = KSCREENWIDTH - 4 * kGAP15 - kAvatarWidth - 40;
        [self.contentView addSubview:self.dateLabel];
        
        // 话题详情
        self.descLabel = [UILabel new];
        [self.contentView addSubview:self.descLabel];
        self.descLabel.textColor = [UIColor c4Color];
        self.descLabel.preferredMaxLayoutWidth = KSCREENWIDTH - kGAP15 - kAvatarWidth;
        self.descLabel.lineBreakMode = NSLineBreakByCharWrapping;
        self.descLabel.numberOfLines = 0;
        self.descLabel.font = [UIFont font18];
        
        //话题类型资源：图片（三种排列样式）、视频（一种排列样式）
        self.jggView = [JGGView new];
        [self.contentView addSubview:self.jggView];
        
        self.playerLayerView = [UIView new];
        [self.contentView addSubview:self.playerLayerView];
        
        [self.playerLayerView addSubview:self.wmplayer];
        self.wmplayer.hidden = YES;
        
        [self.playerLayerView addSubview:self.videoIcon];
        self.videoIcon.hidden = YES;
        
        //关联的作品
        self.artWorksCardView = [IGArtWorksCardView new];
        self.artWorksCardView.backgroundColor = [UIColor mainBackgroundColor];
        self.artWorksCardView.userInteractionEnabled = YES;
        [self.contentView addSubview:self.artWorksCardView];
        
        [self.artWorksCardView setTapActionWithBlock:^{
            XiangQingViewController *vc = [[XiangQingViewController alloc] init];
            vc.p_id = weakSelf.topicModel.product.artId;
            vc.isRoot = YES;
            [UIViewController ig_pushViewController:vc animated:YES];
        }];

        //分享按钮
        self.shareImageView = [[UIImageView alloc] init];
        self.shareImageView.image = [UIImage imageNamed:@"share_parent"];
        self.shareImageView.userInteractionEnabled = YES;
        self.shareImageView.contentMode = UIViewContentModeCenter;
        [self.contentView addSubview:self.shareImageView];
        
        [self.shareImageView setTapActionWithBlock:^{
            if (weakSelf.shareBlock) {
                weakSelf.shareBlock();
            }
        }];
        
        //评论 评论数+评论图标
        self.commentNumLabel = [UILabel new];
        [self.contentView addSubview:self.commentNumLabel];
        self.commentNumLabel.font = [UIFont font12];
        self.commentNumLabel.textColor = [UIColor c5Color];
        self.commentNumLabel.textAlignment = NSTextAlignmentLeft;
        
        self.commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.commentBtn setImage:[UIImage imageNamed:@"评论"] forState:UIControlStateNormal];
        [self.commentBtn setImage:[UIImage imageNamed:@"评论"] forState:UIControlStateSelected];
        [self.contentView addSubview:self.commentBtn];
        
        UIView *commentView = [UIView new];
        commentView.userInteractionEnabled = YES;
        [commentView setTapActionWithBlock:^{
            [weakSelf commentAction:self.commentBtn];
        }];
        [self.contentView addSubview:commentView];
        
        //点赞 点赞数+点赞图标
        self.goodNumLabel = [UILabel new];
        [self.contentView addSubview:self.goodNumLabel];
        self.goodNumLabel.font = [UIFont font12];
        self.goodNumLabel.textColor = [UIColor c5Color];
        self.goodNumLabel.textAlignment = NSTextAlignmentLeft;
        
        self.goodBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.goodBtn setImage:[UIImage imageNamed:@"赞"] forState:UIControlStateNormal];
        [self.goodBtn setImage:[UIImage imageNamed:@"赞_蓝"] forState:UIControlStateSelected];
        [self.contentView addSubview:self.goodBtn];
        
        UIView *goodView = [UIView new];
        goodView.userInteractionEnabled = YES;
        [goodView setTapActionWithBlock:^{
            if (self.likeBlock) {
                self.likeBlock();
            }
        }];
        [self.contentView addSubview:goodView];
        
        //圈子名称 #艺术大家谈
        self.communityLabel = [UILabel new];
        [self.contentView addSubview:self.communityLabel];
        self.communityLabel.textAlignment = NSTextAlignmentLeft;
        self.communityLabel.textColor = [UIColor appColor];
        self.communityLabel.font = [UIFont font14];
        self.communityLabel.userInteractionEnabled = YES;
        
        [self.communityLabel setTapActionWithBlock:^{
            if (!weakSelf.topicModel.circleId || [weakSelf.topicModel.circleId isEqualToString:@""]) {
                return;
            }
            IGCommunityDetailVC *vc = [[IGCommunityDetailVC alloc] initWithCid:weakSelf.topicModel.circleId type:GROUP_TITLE_GROUP_DETAILS];
            [UIViewController ig_pushViewController:vc animated:YES];
        }];
        
        [self.contentView addSubview:self.tableView];
        
        [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(kGAP15);
            make.width.height.mas_equalTo(kAvatarWidth);
        }];
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.headImageView.mas_right).offset(kGAP10);
            make.top.mas_equalTo(self.headImageView);
            make.right.mas_equalTo(-(kGAP15 + kAvatarWidth));
        }];
        [self.followBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(70);
            make.height.mas_equalTo(30);
            make.centerY.mas_equalTo(self.nameLabel.mas_centerY);
            make.right.mas_equalTo(-kGAP15);
        }];
        
        [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.nameLabel.mas_bottom);
            make.left.mas_equalTo(self.headImageView.mas_right).offset(kGAP10);
            make.right.mas_equalTo(-(kGAP15 + kAvatarWidth));
        }];
        [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kGAP15);
            make.right.mas_equalTo(-kGAP15);
            make.top.mas_equalTo(self.headImageView.mas_bottom).offset(kGAP20);
        }];
        [self.jggView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.descLabel);
        }];
        [self.playerLayerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.descLabel);
            make.right.mas_equalTo(self.descLabel);
        }];
        [self.wmplayer mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        [self.videoIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(60);
            make.center.mas_equalTo(self.playerLayerView.center);
        }];
        [self.artWorksCardView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.descLabel);
            make.right.mas_equalTo(self.descLabel);
        }];
        [self.shareImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.commentNumLabel.mas_centerY);
            make.right.mas_equalTo(self.descLabel);
            make.width.height.mas_equalTo(30);
        }];
        [self.commentNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.shareImageView.mas_left).mas_offset(-kGAP10);
            make.top.mas_equalTo(self.artWorksCardView.mas_bottom).offset(kGAP10+kGAP14);
            make.width.height.mas_equalTo(18);
        }];
        [self.commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.commentNumLabel.mas_left).offset(-kGAP6);
            make.top.mas_equalTo(self.commentNumLabel);
            make.width.height.mas_equalTo(18);
        }];
        [commentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_commentBtn.mas_left);
            make.right.mas_equalTo(self.commentNumLabel.mas_right);
            make.top.mas_equalTo(_commentBtn.mas_top).mas_offset(-10);
            make.bottom.mas_equalTo(_commentBtn.mas_bottom).mas_offset(10);
        }];
        [self.goodNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.commentBtn.mas_left).offset(-kGAP6);
            make.top.mas_equalTo(self.commentNumLabel);
            make.width.height.mas_equalTo(18);
        }];
        [self.goodBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.goodNumLabel.mas_left).offset(-kGAP6);
            make.top.mas_equalTo(self.commentNumLabel);
            make.width.height.mas_equalTo(18);
        }];
        [goodView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_goodBtn.mas_left);
            make.right.mas_equalTo(_goodNumLabel.mas_right);
            make.top.mas_equalTo(_goodBtn.mas_top).mas_offset(-10);
            make.bottom.mas_equalTo(_goodBtn.mas_bottom).mas_offset(10);
        }];
        [self.communityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.descLabel);
            make.top.mas_equalTo(self.commentNumLabel);
            make.width.mas_equalTo(150);
            make.height.mas_equalTo(18);
        }];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.jggView);
            make.top.mas_equalTo(self.commentNumLabel.mas_bottom).offset(27);
            make.right.mas_equalTo(-kGAP15);
        }];
        
        self.hyb_lastViewInCell = self.tableView;
        self.hyb_bottomOffsetToCell = kGAP10;
    }
    
    return self;
}

- (void)attentionBtnClick:(UIButton *)sender{
    sender.selected = !sender.isSelected;
    if ([sender isSelected]) {
        sender.layer.borderColor=[UIColor c5Color].CGColor;
    }else{
        sender.layer.borderColor=[UIColor c1Color].CGColor;
    }
    if (self.focusUser) {
        self.focusUser();
    }
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.scrollEnabled = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor clearColor];
        [_tableView registerClass:[IGTopicCommentTableViewCell class] forCellReuseIdentifier:kIGTopicCommentTableViewCell];
        [_tableView registerClass:[IGTopicDetailCommentCell class] forCellReuseIdentifier:kIGTopicDetailCommentCell];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        UIImage *image = [UIImage imageNamed:@"community_comment_bg"];
        image = [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.8];
        _tableView.backgroundView = [[UIImageView alloc] initWithImage:image];
        _tableView.separatorInset = UIEdgeInsetsMake(0, 100, 0, -100);
    }
    return _tableView;
}

//评论按钮
- (void)commentAction:(UIButton *)sender {
    if (self.commentBtnClickBlock) {
        self.commentBtnClickBlock(sender,self.indexPath);
    }
}

- (void)configCellWithModel:(IGTopicModel *)model indexPath:(NSIndexPath *)indexPath {
    self.indexPath = indexPath;
    
    _followBtn.selected = [model.isCollection boolValue];
    if ([_followBtn isSelected]) {
        _followBtn.layer.borderColor=[UIColor c5Color].CGColor;
    }else{
        _followBtn.layer.borderColor=[UIColor c1Color].CGColor;
    }
    
    self.nameLabel.text = model.userNick;
    self.dateLabel.text = model.publishTime;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.userImg] placeholderImage:[UIImage imageNamed:@"place_head_Icon"]];
    self.topicModel = model;
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:model.content];
    [attrString addAttribute:NSFontAttributeName value:[UIFont font18] range:NSMakeRange(0, attrString.length)];
    [attrString addAttribute:NSParagraphStyleAttributeName value:self.muStyle range:NSMakeRange(0, attrString.length)];
    self.descLabel.attributedText = attrString;
    self.descLabel.enabled = YES;//设置文字内容是否可变
    
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont font18], NSParagraphStyleAttributeName:self.muStyle};
    CGFloat h = [model.content boundingRectWithSize:CGSizeMake(KSCREENWIDTH - 2*kGAP15, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height+0.5;
    
    [self.descLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(h);
    }];
    
    CGFloat jjg_height = 0.0;
    CGFloat jjg_width = 0.0;
    BOOL jjg_exist = YES;
    if (model.imgs.count > 0 && model.imgs.count <= 3) {
        jjg_height = [JGGView imageHeight];
        jjg_width  = (model.imgs.count)*([JGGView imageWidth]+kJGG_GAP)-kJGG_GAP;
    } else if (model.imgs.count > 3 && model.imgs.count <= 6){
        jjg_height = 2*([JGGView imageHeight] + kJGG_GAP) - kJGG_GAP;
        jjg_width  = 3*([JGGView imageWidth] + kJGG_GAP) - kJGG_GAP;
    } else  if (model.imgs.count >6 && model.imgs.count <= 9){
        jjg_height = 3*([JGGView imageHeight] + kJGG_GAP) - kJGG_GAP;
        jjg_width  = 3*([JGGView imageWidth] + kJGG_GAP) - kJGG_GAP;
    } else {
        jjg_exist = NO;
    }
    ///解决图片复用问题
    [self.jggView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    ///布局九宫格
    [self.jggView JGGView:self.jggView dataSource:model.imgsBig smallDataSource:model.imgs completeBlock:^(NSInteger index, NSArray *dataSource,NSIndexPath *indexpath) {
        self.tapImageBlock(index,dataSource,self.indexPath);
    }];
    
    [self.jggView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.descLabel.mas_bottom).offset(jjg_exist?kGAP15:0);
        make.size.mas_equalTo(CGSizeMake(jjg_width, jjg_height));
    }];
    
    CGFloat videoHeight = 0.0;
    BOOL video_exist = YES;
    if (model.feedType == FeedTypeMovie) {
        videoHeight = 185.0;
        self.wmplayer.hidden = NO;
        self.videoIcon.hidden = NO;
        [self.wmplayer sd_setImageWithURL:[NSURL URLWithString:model.coverUrl]];
    } else {
        video_exist = NO;
        self.wmplayer.hidden = YES;
        self.videoIcon.hidden = YES;
    }
    
    [self.playerLayerView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.jggView.mas_bottom).offset(video_exist?kGAP15:0);
        make.height.mas_equalTo(videoHeight);
    }];
    
    CGFloat artCardHeight = 0.0;
    BOOL product_exist = YES;
    if (![model.product.artId isEqualToString:@""] && model.product.artId) {
        artCardHeight = 65.0;
        self.artWorksCardView.hidden = false;
        IGArtWorksModel *artWorksModel = model.product;
        [self.artWorksCardView setModel:artWorksModel];
    } else {
        product_exist = NO;
        self.artWorksCardView.hidden = true;
    }
    
    [self.artWorksCardView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.playerLayerView.mas_bottom).offset(product_exist?kGAP15:0);
        make.height.mas_equalTo(artCardHeight);
    }];

    //艺术大家谈
    if (model.circleId && ![model.circleId isEqualToString:@""]) {
        _communityLabel.text = [NSString stringWithFormat:@"#%@#", model.circleName];
        CGFloat comW = [_communityLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 18) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont font14]} context:nil].size.width + 0.5;
        [self.communityLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(comW);
        }];
    } else {
        _communityLabel.text = @"";
    }
    
    //点赞数
    _goodNumLabel.text = model.zanNums;
    CGFloat zanW = [model.zanNums boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 18) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont font12]} context:nil].size.width + 0.5;
    
    [self.goodNumLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(zanW);
    }];
    
    _goodBtn.selected = model.isZan;
    
    //评论数
    _commentNumLabel.text = model.commentNums;
    CGFloat pinglunW = [model.commentNums boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 18) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont font12]} context:nil].size.width + 0.5;
    [self.commentNumLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(pinglunW);
    }];
    
    __weak typeof(self) weakSelf = self;
    CGFloat tableViewHeight = 0.0;
    for (IGCommentBean *commentModel in model.lstComments.count>3?[model.lstComments subarrayWithRange:NSMakeRange(0, 3)]:model.lstComments) {
        CGFloat cellHeight = [IGTopicCommentTableViewCell hyb_heightForTableView:weakSelf.tableView config:^(UITableViewCell *sourceCell) {
            IGTopicCommentTableViewCell *cell = (IGTopicCommentTableViewCell *)sourceCell;
            [cell configCellWithModel:commentModel];
        } cache:^NSDictionary *{
            return @{kHYBCacheUniqueKey : commentModel.commentId,
                     kHYBCacheStateKey : @"",
                     kHYBRecalculateForStateKey : @(YES)};
        }];
        tableViewHeight += cellHeight;
    }
    
    if (model.lstComments.count>3) {
        tableViewHeight += 24;
    }
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(tableViewHeight);
        make.top.mas_equalTo(self.commentNumLabel.mas_bottom).offset(tableViewHeight==0.0?0:27);
    }];
    [self.tableView reloadData];
}

- (UIImageView *)wmplayer {
    if (!_wmplayer) {
        _wmplayer = [[UIImageView alloc] init];
        _wmplayer.contentMode = UIViewContentModeScaleAspectFill;
        _wmplayer.clipsToBounds = YES;
        _wmplayer.backgroundColor = [UIColor mainRandomColor];
    }
    return _wmplayer;
}

- (UIImageView *)videoIcon {
    if (!_videoIcon) {
        _videoIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"topic_video_normal"]];
    }
    return _videoIcon;
}

#pragma mark - UITableViewDataSource, UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    IGCommentBean *model = [self.topicModel.lstComments objectAtIndex:indexPath.row];
    IGTopicCommentTableViewCell *cell = [IGTopicCommentTableViewCell cellWithTableView:tableView];
    NSInteger count = self.topicModel.lstComments.count;
    if (count > 3 && indexPath.row == 3) {
        [cell configShowMoreComment];
    } else {
        [cell configCellWithModel:model];
    }
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = self.topicModel.lstComments.count;
    if (count > 3) {
        return 4;
    }
    
    return count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat cell_height = 0.0f;
    IGCommentBean *model = [self.topicModel.lstComments objectAtIndex:indexPath.row];
    cell_height = [IGTopicCommentTableViewCell hyb_heightForTableView:self.tableView config:^(UITableViewCell *sourceCell) {
        IGTopicCommentTableViewCell *cell = (IGTopicCommentTableViewCell *)sourceCell;
        [cell configCellWithModel:model];
    } cache:^NSDictionary *{
        NSDictionary *cache = @{kHYBCacheUniqueKey : model.commentId,
                                kHYBCacheStateKey : @"",
                                kHYBRecalculateForStateKey : @(model.shouldUpdateCache)};
        model.shouldUpdateCache = NO;
        return cache;
    }];
    return cell_height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.indexPath = indexPath;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    IGCommentBean *commentModel = [self.topicModel.lstComments objectAtIndex:indexPath.row];
    CGFloat cell_height = 0.0;
    cell_height = [IGTopicCommentTableViewCell hyb_heightForTableView:self.tableView config:^(UITableViewCell *sourceCell) {
        IGTopicCommentTableViewCell *cell = (IGTopicCommentTableViewCell *)sourceCell;
        [cell configCellWithModel:commentModel];
    } cache:^NSDictionary *{
        NSDictionary *cache = @{kHYBCacheUniqueKey : commentModel.commentId,
                                kHYBCacheStateKey : @"",
                                kHYBRecalculateForStateKey : @(NO)};
        commentModel.shouldUpdateCache = NO;
        return cache;
    }];
    
    if (indexPath.row == 3) {
        IGTopicDetailVC *detailVC = [[IGTopicDetailVC alloc] initWithTopicId:self.topicModel.tid];
        [UIViewController ig_pushViewController:detailVC animated:YES];
        return;
    }
    if ([self.delegate respondsToSelector:@selector(passCellHeight:commentModel:commentCell:messageCell:)]) {
        IGTopicCommentTableViewCell *commetCell = (IGTopicCommentTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        [self.delegate passCellHeight:cell_height commentModel:commentModel commentCell:commetCell messageCell:self];
    }
}

- (NSMutableParagraphStyle *)muStyle {
    if (!_muStyle) {
        _muStyle = [[NSMutableParagraphStyle alloc]init];
        _muStyle.lineSpacing = 3;    //设置行间距离
        _muStyle.alignment = NSTextAlignmentLeft;//对齐方式
    }
    return _muStyle;
}
- (void)setFrame:(CGRect)frame {
    frame.size.height -= 10;
    [super setFrame:frame];
}

@end
